﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;
using System.Configuration;

namespace Testify.Account
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
                 //RegisterHyperLink.NavigateUrl = "Register.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
            foreach (string i in Session.Contents)
            {
                if (i == "isTakingTest")
                    if ((bool)Session["isTakingTest"] == true)
                        Response.Redirect("~/Tests/TakeTest.aspx");
            }

            (this.LoginUser.FindControl("UserName") as TextBox).Focus();
        }

        protected void LoginButton_Click(object sender, EventArgs e)
        {

        }

        protected Boolean login()
        {
            Boolean loggedIn = false;
            this.LoginUser.UserName = this.LoginUser.UserName.ToLower();

            OracleConnection myConnection1 = new OracleConnection(ConfigurationManager.ConnectionStrings["Singularity"].ConnectionString);
            OracleCommand loginCommand = new OracleCommand("singularity.users.credentialsmatch", myConnection1);
            loginCommand.CommandType = CommandType.StoredProcedure;
            loginCommand.BindByName = true;
            loginCommand.Parameters.Clear();
            loginCommand.Parameters.Add("p_UserName", this.LoginUser.UserName.ToString());
            loginCommand.Parameters.Add("p_UserPassword", this.LoginUser.Password.ToString());
            loginCommand.Parameters.Add("p_IP", System.Net.Dns.GetHostAddresses(System.Net.Dns.GetHostName()).GetValue(1).ToString());
            loginCommand.Parameters.Add("p_HostName", System.Net.Dns.GetHostName().ToUpper());
            loginCommand.Parameters.Add("ReturnValue", OracleDbType.Int32).Direction = ParameterDirection.ReturnValue;
 
            try
            {
                myConnection1.Open();

                loginCommand.ExecuteNonQuery();

                string test = loginCommand.Parameters["ReturnValue"].Value.ToString();

                if (loginCommand.Parameters["ReturnValue"].Value.ToString() != "null")
                {
                    Session["UserID"] = loginCommand.Parameters["ReturnValue"].Value.ToString();
                    //Session["user_Type"] = loginCommand.Parameters["p_userTypeID"].Value.ToString();
                    loggedIn = true;
                }
                else
                {
                    Session["UserID"] = null;
                    //Session["user_Type"] = null;
                    loggedIn = false;
                }

                Panel lit = LoginUser.FindControl("failureBox") as Panel;
                lit.Visible = true;
            }
            catch (Exception ex)
            {
                Panel lit = LoginUser.FindControl("failureBox") as Panel;
                lit.Visible = true;

                this.LoginUser.FailureText = "ERROR: could not login." + ex.Message;
            }
            finally
            {
                myConnection1.Close();
            }

            return loggedIn;
        }

        protected void LoginUser_Authenticate1(object sender, AuthenticateEventArgs e)
        {
            if (!String.IsNullOrEmpty(this.LoginUser.UserName) && !String.IsNullOrEmpty(this.LoginUser.Password))
            {
                if (login())
                {
                    Session["UserName"] = this.LoginUser.UserName;
                    e.Authenticated = true;
                }
                else
                {
                    e.Authenticated = false;
                }
            }
            else
            {
                Panel lit = LoginUser.FindControl("failureBox") as Panel;
                lit.Visible = true;

                this.LoginUser.FailureText = "UserName and Password are both required to login";
            }
        }

    }
}
